<?php

class SnController extends Zend_Controller_Action
{
    protected $db;
    
    public function init()
    {

        $session = new Zend_Session_Namespace('Zend_Auth_Admin');
        $this->sess = $session->data;

        $this->_helper->layout->setLayout('sn');

        if(!isset($this->sess)){
            $param = array(
            'action' => 'admin',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }

        $this->admin = new Application_Model_Admin();
        
        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        $session->action = $this->param['action'];

    }

    public function indexAction()
    {

        //

    }
    


    public function gensnAction()
    {

        //

    }

    public function cetaksnAction()
    {

        //


    }

    public function aktifasisnAction()
    {

        if ($this->getRequest()->isPost()) {
            $formData  = $this->_request->getPost();


            $sn   = $formData['sn'];
            $snList   = $formData['snList'];
            $flag   = $formData['flag'];
            
            $err = 0;
            $this->view->errorMsg = "";

            /*
             * Check List
             */
            if ($snList == "" && $flag == 1){
                $this->view->errorMsg .= "* SN harus dipilih.<br>";
                //return;
                $err = 1;
            }


            $msg = "";
            if ($err == 0){

                    $snm = new Application_Model_Sn();
                    $dataT = $snm->aktivasiSN($snList, $flag, $sn);
                    $msg = $dataT["msg"];
                    $code = $dataT['code'];

                    $this->view->code = $code;

                    if($code == 0){
                        $param = array(
                        'action' => 'aktifasisn',
                        'controller' => 'sn',
                        'code' => $code,
                        'msg' => $msg
                        );

                        $this->_redirect($this->view->url($param, 'default', true));
                    }else{
                        $this->view->errorMsg = $msg;
                        $this->view->data = $this->param;
                    }

            }else{

                //$form = new Application_Form_Accounts_Transfer();
                //$sess = new Zend_Session_Namespace();

                //$form->populate($this->param);

                //$this->view->form = $form;

                $this->view->code = 1;
                $this->view->data = $this->param;
            }


            //$this->view->id = $msg;

        }else{

            if (isset($this->param['code']) && $this->param['code'] == 0){
                $this->view->id = $this->param['msg'];
                $this->view->code = $this->param['code'];
            }else{
                //$id = $this->param['id'];

                //$data = $this->admin->detailWithdraw($id);


                //$form->populate($this->param);

                //$this->view->form = $form;

                $this->view->code = 1;
                //$this->view->data = $data;
            }
        } 
    }


   

}

